package com.edais.service.imp;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.edais.actions.PerMatchDetailAction;
import com.edais.dao.WinDao;
import com.edais.domain.WinUser;
import com.edais.service.WinService;
import com.edais.vo.WinVo;

@Service

@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public class WinServiceImp implements WinService {
	private static final Log logger = LogFactory.getLog(PerMatchDetailAction.class);

	@Autowired
	private WinDao winDao;

	/**
	 * 获取已获得奖励列表
	 * @param 用户id
	 * @param 当前页
	 * @param 页大小
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	public Map<String, Object> getMyWinList(String userId, Integer current, Integer pageSize) throws Exception{
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		List<WinVo> list = new ArrayList<WinVo>();
		
		Integer rowBegin = (current - 1) * pageSize;
		Integer rowEnd = current * pageSize;
		
		// 获取已获得奖励总数
		
		Map map = this.winDao.getMyWinTotalNum(userId);
		String totalNum = map.get("COUNT")+"";
		String winTotal = map.get("WINTOTAL")+"";
		
		if (Integer.valueOf(totalNum) > 0) {
			// 获取已获得奖励列表
			List<WinUser> tmpList = this.winDao.getMyWinList(userId, rowBegin, rowEnd);
			for (WinUser winUser : tmpList) {
				WinVo winVo = new WinVo();
				// 活动名称
				winVo.setActivityName(winUser.getActivityName());
				// 活动时间
				winVo.setActivityTime(this.changeDateFormat(winUser.getBeginTime()) + "-" + this.changeDateFormat(winUser.getEndTime()));
				// 获得奖励
				winVo.setWin(winUser.getAmount().setScale(2, BigDecimal.ROUND_DOWN).toString());
				// 返还时间
				winVo.setReturnTime(this.changeDateFormat(winUser.getCreateTime()));
				// 备注
				winVo.setMemo(winUser.getMemo());
				
				list.add(winVo);
			}
		}
		
		rtnMap.put("totalNum", totalNum);
		rtnMap.put("winTotal", winTotal);
		rtnMap.put("listSize", list.size());
		rtnMap.put("list", list);
		
		return rtnMap;
	}
	
	/**
	 * 转换日期格式
	 * @param date
	 */
	private String changeDateFormat(Date date) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");//设置日期格式
		
		return sdf.format(date);
	}
}
